#delimit;
clear all;
set more off;



use "Prec_Level_TO.dta";


xtset precinct_county_ID runoff;

gen fd_to_perc = to_perc - l.to_perc;
gen fd_alt_to_perc = alt_to_perc - l.alt_to_perc; 


lowess fd_alt_to_perc trump_2pvs, gen(fd_alt_to_perc_hat);

twoway  (scatter fd_alt_to_perc trump_2pvs if fd_alt_to_perc > -0.15 & fd_alt_to_perc < 0.05 , msize(tiny) msymbol(oh)) 
(lpolyci fd_alt_to_perc trump_2pvs,  ciplot(rline)) ,
title(" ") ytitle("{&Delta} in Precinct Turnout" "between Runoff and General") xtitle("Trump Vote Share in General")  graphregion(color(white)) bgcolor(white)
legend(off);

graph export "Alt_TO_PrecinctLevel_RO_Lowess.pdf", replace;






gen runoff_by_trump_2pvs_s = runoff*std_trump_2pvs;
gen runoff_by_collins_share_s = runoff*std_collins_share;
gen runoff_by_collins_rep_share_s = runoff*std_collins_rep_share;
gen runoff_by_loeffler_share_s = runoff*std_loeffler_share;
gen runoff_by_loeffler_rep_share_s = runoff*std_loeffler_rep_share;

gen runoff_by_tp_resid_s = runoff*std_tp_vs_resid;
gen runoff_trump_hat_rf_s = runoff*std_trump_hat_rf;

label variable runoff_by_trump_2pvs_s "Trump VS";
label variable runoff_by_collins_share_s "Collins VS";
label variable runoff_by_collins_rep_share_s "Collins Rep. Cand. VS";
label variable runoff_by_loeffler_share_s "Loeffler VS";
label variable runoff_by_loeffler_rep_share_s "Loeffler Rep. Cand. VS";

label variable runoff_by_tp_resid_s "Trump-Perdue VS Resid.";
label variable runoff_trump_hat_rf_s "Est. Share Trump Approvers";

*Add Valdosta and Dalton County Indicators; 
gen valdosta_msa = 0;
replace valdosta_msa = 1 if county == "Lowndes" | county == "Brooks" | county == "Echols" | county == "Lanier";

gen dalton_msa = 0;
replace dalton_msa = 1 if county == "Murray" | county == "Whitfield";

gen ro_valdosta_msa = valdosta_msa*runoff;
gen ro_dalton_msa = dalton_msa*runoff;


xtreg alt_to_perc_ runoff runoff_by_trump_2pvs_s, fe robust cluster(precinct_county_ID);
est store tvs;


xtreg alt_to_perc_ runoff runoff_by_trump_2pvs_s ro_valdosta_msa ro_dalton_msa, fe robust cluster(precinct_county_ID);
est store tvs_rally_inds;


xtreg alt_to_perc_ runoff runoff_by_collins_share_s, fe robust cluster(precinct_county_ID);
est store cvs;

xtreg alt_to_perc_ runoff runoff_by_collins_share_s ro_valdosta_msa ro_dalton_msa, fe robust cluster(precinct_county_ID);
est store cvs_rally_inds;


xtreg alt_to_perc_ runoff runoff_by_collins_rep_share_s, fe robust cluster(precinct_county_ID);
est store crepvs;

xtreg alt_to_perc_ runoff runoff_by_collins_rep_share_s ro_valdosta_msa ro_dalton_msa, fe robust cluster(precinct_county_ID);
est store crepvs_rally_inds;



xtreg alt_to_perc_ runoff runoff_by_tp_resid_s, fe robust cluster(precinct_county_ID);
est store tpresid;

xtreg alt_to_perc_ runoff runoff_by_tp_resid_s ro_valdosta_msa ro_dalton_msa, fe robust cluster(precinct_county_ID);
est store tpresid_rally_inds;

xtreg alt_to_perc_ runoff runoff_trump_hat_rf_s, fe robust cluster(precinct_county_ID);
est store t_hat_rf;

xtreg alt_to_perc_ runoff runoff_trump_hat_rf_s ro_valdosta_msa ro_dalton_msa, fe robust cluster(precinct_county_ID);
est store t_hat_rf_rally_inds;



#delimit;
coefplot (tvs, keep(runoff_by_trump_2pvs_s)  msymbol(X) mcolor(maroon) offset(0.075))  
(cvs, offset(0.075) keep(runoff_by_collins_share_s) msymbol(X) mcolor(maroon)) 
(crepvs, offset(0.075) keep(runoff_by_collins_rep_share_s) msymbol(X) mcolor(maroon))
(tpresid,offset(0.075)  keep(runoff_by_tp_resid_s) msymbol(X) mcolor(maroon))
(t_hat_rf, offset(0.075) keep(runoff_trump_hat_rf_s) msymbol(X) mcolor(maroon)), xline(0) graphregion(color(white)) bgcolor(white) 
legend(order(2) label(2 "Point Estimate"))  title("Turnout in Runoff") ciopts(recast(rcap) lcolor(maroon));

graph export "Alt_TO_PrecinctLevel_FEs_2_PB.pdf", replace;



*Adjusting Standard Errors for Spatial Correlation with Conley SEs;
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store tvs;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store tvs_rally_inds;


ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_trump_2pvs_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store cvs;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store cvs_rally_inds;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);


ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store crepvs;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store crepvs_rally_inds;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_collins_rep_share_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store tpresid;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store tpresid_rally_inds;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_by_tp_resid_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);


ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store t_hat_rf;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(1.60934);
est store t_hat_rf_rally_inds;

ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(3.21869);
ols_spatial_HAC d.alt_to_perc_ d.runoff d.runoff_trump_hat_rf_s d.ro_valdosta_msa d.ro_dalton_msa, lat(lat_num) lon(long_num) timevar(runoff) panelvar(precinct_county_ID) dist(8.04672);

#delimit;
coefplot (tvs, keep(D.runoff_by_trump_2pvs_s)  msymbol(X) mcolor(maroon) offset(0.075) ) (tvs_rally_inds, offset(-0.075) keep(D.runoff_by_trump_2pvs_s) msymbol(T) mcolor(maroon)) 
(cvs, offset(0.075) keep(D.runoff_by_collins_share_s) msymbol(X) mcolor(maroon)) (cvs_rally_inds, offset(-0.075) keep(D.runoff_by_collins_share_s) msymbol(T) mcolor(maroon)) 
(crepvs, offset(0.075) keep(D.runoff_by_collins_rep_share_s) msymbol(X) mcolor(maroon)) (crepvs_rally_inds, offset(-0.075) keep(D.runoff_by_collins_rep_share_s) msymbol(T) mcolor(maroon))
(tpresid,offset(0.075)  keep(D.runoff_by_tp_resid_s) msymbol(X) mcolor(maroon)) (tpresid_rally_inds, offset(-0.075) keep(D.runoff_by_tp_resid_s) msymbol(T) mcolor(maroon)) 
(t_hat_rf, offset(0.075) keep(D.runoff_trump_hat_rf_s) msymbol(X) mcolor(maroon)) (t_hat_rf_rally_inds, offset(-0.075) keep(D.runoff_trump_hat_rf_s) msymbol(T) mcolor(maroon)) , 
xline(0) graphregion(color(white))  bgcolor(white)
legend(order(2 4)  label(2 "Base Model") label(4  "Rally MSAs X Runoff")) title("Turnout in Runoff" "(Conley SEs, 1 Mile Kernel Cutoff)") ciopts(recast(rcap) lcolor(maroon));

graph export "Alt_TO_PrecinctLevel_FEs_ConleySEs.pdf", replace;








#delimit;
sum runoff_by_trump_2pvs_s if runoff == 1;
local min_trump_2pvs_s = `r(min)';

*Total Perdue Votes as DV;
xtreg tot_perdue_votes_ runoff runoff_by_trump_2pvs_s, fe robust cluster(precinct_county_ID);
est store tvs;

*Counterfactual Calculation;
*Final Result: Ossoff 2,269,923 and Perdue 2,214,979;
*Realized Vote Margin was 54,944;
predict fit_tot_perdue_votes if runoff == 1, xb;
local scaling_factor = 1;

gen fit_tot_perdue_votes_counter =  _b[_cons] +  _b[runoff] + (_b[runoff_by_trump_2pvs_s]*`scaling_factor')*(`min_trump_2pvs_s') if runoff == 1;
egen tot_counter = sum(fit_tot_perdue_votes_counter);
egen tot_fit = sum(fit_tot_perdue_votes);

tab tot_counter; 
tab tot_fit;
gen delta = tot_counter- tot_fit;
tab delta;

gen scaling_factor_tie = (2269738/2651 -  _b[_cons] -  _b[runoff])/(_b[runoff_by_trump_2pvs_s]*(`min_trump_2pvs_s')); 
tab scaling_factor_tie;




*Dropping Precincts with Fewer than 100 Registered Voters;

gen sample_incl = 1 if reg_voters >= 100 | reg_voters_vf >= 100;

xtreg alt_to_perc_ runoff runoff_by_trump_2pvs_s if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store tvs;

xtreg alt_to_perc_ runoff runoff_by_trump_2pvs_s ro_valdosta_msa ro_dalton_msa if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store tvs_rally_inds;

xtreg alt_to_perc_ runoff runoff_by_collins_share_s if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store cvs;

xtreg alt_to_perc_ runoff runoff_by_collins_share_s ro_valdosta_msa ro_dalton_msa if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store cvs_rally_inds;


xtreg alt_to_perc_ runoff runoff_by_collins_rep_share_s if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store crepvs;

xtreg alt_to_perc_ runoff runoff_by_collins_rep_share_s ro_valdosta_msa ro_dalton_msa if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store crepvs_rally_inds;

xtreg alt_to_perc_ runoff runoff_by_tp_resid_s if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store tpresid;

xtreg alt_to_perc_ runoff runoff_by_tp_resid_s ro_valdosta_msa ro_dalton_msa if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store tpresid_rally_inds;


xtreg alt_to_perc_ runoff runoff_trump_hat_rf_s if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store t_hat_rf;

xtreg alt_to_perc_ runoff runoff_trump_hat_rf_s ro_valdosta_msa ro_dalton_msa if sample_incl == 1, fe robust cluster(precinct_county_ID);
est store t_hat_rf_rally_inds;

drop sample_incl;

#delimit;


#delimit;
coefplot (tvs, offset(0.075) keep(runoff_by_trump_2pvs_s)  msymbol(X) mcolor(maroon) ) 
(cvs, offset(0.075) keep(runoff_by_collins_share_s) msymbol(X) mcolor(maroon)) 
(crepvs, offset(0.075) keep(runoff_by_collins_rep_share_s) msymbol(X) mcolor(maroon)) 
(tpresid, offset(0.075) keep(runoff_by_tp_resid_s) msymbol(X) mcolor(maroon)) 
(t_hat_rf, offset(0.075) keep(runoff_trump_hat_rf_s) msymbol(X) mcolor(maroon)) , 
xline(0) graphregion(color(white))  bgcolor(white) xscale(r(-0.02 0))
legend(order(2)  label(2 "Point Estimate")) title("Turnout in Runoff" "Precincts with >= 100 Reg. Voters" ) ciopts(recast(rcap) lcolor(maroon));

graph export "Alt_TO_PrecinctLevel_FEs_2_DropSmallPrec_PB.pdf", replace;


*Lagged DV Specifications;
reg alt_to_perc l.alt_to_perc runoff_by_trump_2pvs_s, robust cluster(precinct_county_ID);
est store tvs;

reg alt_to_perc l.alt_to_perc runoff_by_trump_2pvs_s ro_valdosta_msa ro_dalton_msa, robust cluster(precinct_county_ID);
est store tvs_rally_inds;

reg alt_to_perc l.alt_to_perc runoff_by_collins_share_s, robust cluster(precinct_county_ID);
est store cvs;

reg alt_to_perc l.alt_to_perc runoff_by_collins_share_s ro_valdosta_msa ro_dalton_msa, robust cluster(precinct_county_ID);
est store cvs_rally_inds;

reg alt_to_perc l.alt_to_perc runoff_by_collins_rep_share_s, robust cluster(precinct_county_ID);
est store crepvs;

reg alt_to_perc l.alt_to_perc runoff_by_collins_rep_share_s ro_valdosta_msa ro_dalton_msa, robust cluster(precinct_county_ID);
est store crepvs_rally_inds;

reg alt_to_perc l.alt_to_perc runoff_by_tp_resid_s, robust cluster(precinct_county_ID);
est store tpresid;

reg alt_to_perc l.alt_to_perc runoff_by_tp_resid_s ro_valdosta_msa ro_dalton_msa, robust cluster(precinct_county_ID);
est store tpresid_rally_inds;

reg alt_to_perc l.alt_to_perc runoff_trump_hat_rf_s, robust cluster(precinct_county_ID);
est store t_hat_rf;

reg alt_to_perc l.alt_to_perc runoff_trump_hat_rf_s ro_valdosta_msa ro_dalton_msa, robust cluster(precinct_county_ID);
est store t_hat_rf_rally_inds;


coefplot (tvs, keep(runoff_by_trump_2pvs_s)  msymbol(X) mcolor(maroon) offset(0.075) ) 
(cvs, offset(0.075) keep(runoff_by_collins_share_s) msymbol(X) mcolor(maroon)) 
(crepvs, offset(0.075) keep(runoff_by_collins_rep_share_s) msymbol(X) mcolor(maroon)) 
(tpresid,offset(0.075)  keep(runoff_by_tp_resid_s) msymbol(X) mcolor(maroon)) 
(t_hat_rf, offset(0.075) keep(runoff_trump_hat_rf_s) msymbol(X) mcolor(maroon)) , 
xline(0) graphregion(color(white))  bgcolor(white) xscale(r(-0.02 0))
legend(order(2)  label(2 "Point Estimate")) title("Turnout in Runoff" "Lagged DV Specification") ciopts(recast(rcap) lcolor(maroon));

graph export "Alt_TO_PrecinctLevel_LDV_PB.pdf", replace;
